package com.ruoyi.reservationserver.controller;


import com.ruoyi.reservationserver.entity.Dynamic;
import com.ruoyi.reservationserver.service.DynamicService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 动态新闻表(Dynamic)表控制层
 *
 * @author makejava
 * @since 2023-07-04 16:21:48
 */
//@RestController
//@RequestMapping("dynamic")
public class DynamicController {
    /**
     * 服务对象
     */
    @Resource
    private DynamicService dynamicService1;


    /**
     * 获取动态新闻列表
     * @return
     */
    @GetMapping("/getDynamic")
    public Result getDynamic(){

        List<Dynamic> dynamics = dynamicService1.list();
        if (dynamics.isEmpty()){
            return Result.error("不存在新聞");
        }else {
            return Result.success(dynamics);
        }
    }

    /**
     * 获取动态新闻列表数量
     * @return
     */
    @GetMapping("/getDynamicNum")
    public Result getDynamicNum(){
        Result result = new Result();
        int count = dynamicService1.count();
        if (count <0){
            return Result.error("500","错误");
        }else {
            return Result.success(count);
        }
    }


    /**
     * 新增数据
     *
     * @param dynamic 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Dynamic dynamic) {
        if (dynamic==null){
            return Result.error("数据不能为空。");
        }
        return Result.success(this.dynamicService1.insert(dynamic));
    }

    /**
     * 编辑数据
     *
     * @param dynamic 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Dynamic dynamic) {
        System.out.println(dynamic);
        if (dynamic==null){
            return Result.error("返回数据不能为空");
        }
        this.dynamicService1.update(dynamic);
        return Result.success();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("dynamicId") Integer id) {

        try {

           if(this.dynamicService1.deleteById(id)){
               return Result.success();
           }else {
               return Result.error("删除失败");
           }

        }catch (Exception e) {
            System.out.println(e);
            return Result.error("删除异常");
        }

    }


}

